package Format;

import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

/**
 * @Author MJS
 * @Date 2021/8/5 10:20
 * @Description
 */
public class DateFormat {
    public static void main(String[] args) {
        //字符串的使用
        String ab = "马佳帅";
        String str = String.format("确认卸货完成后，向发货人%s，索要由智运通发出短信内的四位数卸货验证码。", ab);
        System.out.println(str);
        //日期相关的
        Date date = new Date();
        System.out.println(date);
        //c的使用   星期四 八月 05 11:34:18 CST 2021
        System.out.println(String.format("全部日期和时间信息：%tc", date));
        //F的使用 2021-08-05
        System.out.println(String.format("年-月-日：%tF", date));
        //D的使用   08/05/21
        System.out.println(String.format("月/日/年：%tD", date));
        //r的使用 11:34:18 上午
        System.out.println(String.format("HH:MM:SS PM：%tr", date));
        //T的使用 11:34:18
        System.out.println(String.format("HH:MM:SS：%tT", date));
        //R的使用 11:34
        System.out.println(String.format("HH:MM：%tR", date));
        //F+T的使用
        System.out.println(String.format("年-月-日 HH:MM:SS: %tF %<tT ", date));
        //F+R的使用
        System.out.println(String.format("年-月-日 HH:MM: %tF %<tR ", date));
        //自定义格式的日期转换
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM-dd HH:mm:ss");
        System.out.println(simpleDateFormat.format(date));
        System.out.println(simpleDateFormat2.format(date));
        System.out.println(simpleDateFormat3.format(date));


        System.out.println("----------------------------------------------------");
        //若字符串前面是0的话   省略不写
        String a = "01";
        String b = "10";
        System.out.println(a.replaceFirst("0*", "") + "/" + b + "日装");
        System.out.println(b.replaceFirst("0*", "") + "/" + b + "日装");
        System.out.println(b.replaceFirst("0*", "") + "月" + b + "日装");

        System.out.println("-----------------------------------------------------");


        System.out.println(String.format("%tF %<tT", date));
        date.setTime(date.getTime() - 60 * 1000);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));

        Date date1 = new Date();
        String processTime = String.format("%tF %<tT", date1);
        date1.setTime(date1.getTime() - 60 * 1000);
        String startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);
        date1.setTime(date1.getTime() + 4 * 60 * 1000);
        String endTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1);
        System.out.println("startTime:" + startTime);
        System.out.println("processTime:" + processTime);
        System.out.println("endTime:" + endTime);


        System.out.println("----------------------------------------------------");

        long l = System.currentTimeMillis();
        long l2 = l + 8 * 60 * 60 * 1000;
        long l3 = l2 + 60 * 1000;
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(l);
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(l2);
        String format3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(l3);
        System.out.println(format);
        System.out.println(format1);
        System.out.println(format3);

        long l1 = System.currentTimeMillis() / 1000;
        String format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(l1);
        System.out.println(format2);

    }

    @Test
    public void now() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);
        System.out.println(now.toLocalTime());
        System.out.println(now.toLocalDate());
        System.out.println(now.getDayOfYear());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getDayOfWeek());
        System.out.println(now.plusDays(-1).toLocalDate());

    }

    @Test
    public void ce() {
        Date date = new Date();
        String StrDate = String.format("%tF %<tT", date);
        long time = date.getTime();
        System.out.println(time);
        System.out.println(StrDate);
        System.out.println(date);
        System.out.println(new Date(time + 25000));

    }

    @Test
    public void t() {
        System.out.println(String.format("%02d", 3));
        System.out.println(String.format("%02d", 12));
        String a = "2022.02.01-2023.03.05";

        String b = a.substring(0, a.indexOf("-")).replace(".", "-");
        String c = a.substring(a.indexOf("-") + 1, a.length()).replace(".", "-");
        System.out.println(b);
        System.out.println(c);
    }

    @Test
    public void stringFormat() {
        ArrayList<Object> list = Lists.newArrayList();
        list.add("两线四轴");
        list.add("二轴");
        list.add("三线六轴");
        list.add("三轴");
        list.add("后二桥");
        System.out.println(list);
        String join = StringUtils.join(list.toArray());
        String join1 = StringUtils.join(list.toArray(), ",");
        System.out.println(join);
        System.out.println(join1);
        String fJoin = String.format("[%s]", join);
        System.out.println(fJoin);

        String format = String.format("[%s]", join1);
        System.out.println(format);
        Iterator<Object> iterator = list.iterator();

        String join2 = String.join(",", "a", "b", "c");
        System.out.println(join2);
    }

    @Test
    public void stringFormat2(){
        System.out.println(String.format("%.5f", 13.341234512));//四舍五入 %.xf 只要小数点后面的x位数

    }

    @Test
    public void testLocalDateTime(){
        long now = System.currentTimeMillis() / 1000;
        LocalDateTime current = Instant.ofEpochSecond(now).atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime t1 = current.minusHours(1), t2 = t1.minusHours(1), t3 = t2.minusHours(1);


        Date date = new Date();
        long l = date.toInstant().toEpochMilli();
        System.out.println(l);

//        LocalDateTime current = Instant.now().atZone(ZoneId.systemDefault()).toLocalDateTime();
//        LocalDateTime t1 = current.minusHours(1), t2 = t1.minusHours(1), t3 = t2.minusHours(1);

        System.out.println(t1);
        System.out.println(t2);
        System.out.println(t3);

    }

    @Test
    public void phoneTest(){
        String phone = "18231157516";
        String s = phone.replaceAll("(\\d{2})\\d{6}(\\d{3})", "$1******$2");
        System.out.println(s);


    }
}
